package com.ruoyi.jinchuguanli.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.ruoyi.cangku.mapper.ShangpinguanliMapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.jinchuguanli.domain.Xiaoshouchuku;
import com.ruoyi.jinchuguanli.domain.XiaoshouchukuReturn;
import com.ruoyi.jinchuguanli.service.IXiaoshouchukuService;
import net.sf.jasperreports.engine.JRException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * chukuController
 *
 * @author ruoyi
 * @date 2023-11-08
 */
@Controller
@RequestMapping("/jinchuguanli/chuku")
public class XiaoshouchukuController extends BaseController {
    private final String prefix = "jinchuguanli/chuku";

    @Autowired
    private IXiaoshouchukuService xiaoshouchukuService;

    @Autowired
    private ShangpinguanliMapper shangpinguanliMapper;

    @RequiresPermissions("jinchuguanli:chuku:view")
    @GetMapping()
    public String chuku () {
        return prefix + "/chuku";
    }

    /**
     * 查询chuku列表
     */
    @RequiresPermissions("jinchuguanli:chuku:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list (Xiaoshouchuku xiaoshouchuku,Integer pageNum,Integer pageSize) {
        startPage();
        List<XiaoshouchukuReturn> list = new ArrayList<>();
        PageInfo<Xiaoshouchuku> xiaoshouchukuPageInfo =
                PageUtils.startPage(pageNum,pageSize).doSelectPageInfo(()->
                        xiaoshouchukuService.selectXiaoshouchukuList(xiaoshouchuku));
        PageInfo<XiaoshouchukuReturn> xiaoshouchukuReturnPageInfo = new PageInfo<>();
        BeanUtils.copyProperties(xiaoshouchukuPageInfo,xiaoshouchukuReturnPageInfo);

        List<Xiaoshouchuku> xiaoshouchukuList = xiaoshouchukuPageInfo.getList();
        for (Xiaoshouchuku xc:xiaoshouchukuList
             ) {
            XiaoshouchukuReturn xiaoshouchukuReturn = xiaoshouchukuService.aReturnFormat(xc);
            list.add(xiaoshouchukuReturn);
        }
        xiaoshouchukuReturnPageInfo.setList(list);

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(xiaoshouchukuReturnPageInfo.getTotal());

        return rspData;
    }

    /**
     * 导出chuku列表
     */
    @RequiresPermissions("jinchuguanli:chuku:export")
    @Log(title = "出库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export (Xiaoshouchuku xiaoshouchuku) {
        List<XiaoshouchukuReturn> list = xiaoshouchukuService.selectXiaoshouchukuReturnList(xiaoshouchuku);
        ExcelUtil<XiaoshouchukuReturn> util = new ExcelUtil<XiaoshouchukuReturn>(XiaoshouchukuReturn.class);
        return util.exportExcel(list, "出库数据");
    }

    /**
     * 新增chuku
     */
    @GetMapping("/add")
    public String add () {
        return prefix + "/add";
    }

    /**
     * 新增保存chuku
     */
    @RequiresPermissions("jinchuguanli:chuku:add")
    @Log(title = "出库", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave (Xiaoshouchuku xiaoshouchuku) throws JRException, IOException {

        int re = xiaoshouchukuService.insertXiaoshouchuku(xiaoshouchuku);
        if (re == 6) {
            return toAjax("商品门扇与门框非同一生产厂家");
        }
        xiaoshouchuku.setId((long) xiaoshouchukuService.selectIdForNewOne());
        String s = JasperUtils.ChuKuDanSet(xiaoshouchuku,shangpinguanliMapper);
        if (s == null) {
            return toAjax(-1);
        }
        return toAjax(re);
    }

    /**
     * 修改chuku
     */
    @RequiresPermissions("jinchuguanli:chuku:edit")
    @GetMapping("/edit/{id}")
    public String edit (@PathVariable("id") Long id, ModelMap mmap) {
        Xiaoshouchuku xiaoshouchuku = xiaoshouchukuService.selectXiaoshouchukuById(id);
        XiaoshouchukuReturn xiaoshouchukuReturn = xiaoshouchukuService.aReturnFormat(xiaoshouchuku);
        String jsonStr = JSONObject.toJSONString(xiaoshouchukuReturn.getShangpin());
        xiaoshouchuku.setShangpinbianhao(jsonStr);
        mmap.put("xiaoshouchuku", xiaoshouchuku);
        return prefix + "/edit";
    }

    /**
     * 修改保存chuku
     */
    @RequiresPermissions("jinchuguanli:chuku:edit")
    @Log(title = "chuku", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave (Xiaoshouchuku xiaoshouchuku) throws JRException, IOException {
        String s = JasperUtils.ChuKuDanSet(xiaoshouchuku, shangpinguanliMapper);
        if (s == null) {
            return toAjax(-1);
        }
        int re = xiaoshouchukuService.updateXiaoshouchuku(xiaoshouchuku);
        if (re == 6) {
            return toAjax("商品门扇与门框非同一生产厂家");
        }
        return toAjax(re);
    }

    /**
     * 删除chuku
     */
    @RequiresPermissions("jinchuguanli:chuku:remove")
    @Log(title = "chuku", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove (String ids) {
        return toAjax(xiaoshouchukuService.deleteXiaoshouchukuByIds(ids));
    }
}
